/*
 * @Description: 聊天气泡
 * @Author: iamsmiling
 * @Date: 2021-06-01 22:00:42
 * @LastEditTime: 2021-06-02 13:23:20
 */
import 'package:credit_business_circle/R/R.dart';
import 'package:credit_business_circle/domain/remote/message/chat_record_entity.dart';
import 'package:flutter/material.dart';

class ChatBubble extends StatelessWidget {
  final ChatRecordEntity record;
  const ChatBubble({Key? key, required this.record}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      child: record.isMe
          ? _ChatBubbleMe(record: record)
          : _ChatBubbleOther(record: record),
    );
  }
}

class _ChatBubbleMe extends StatelessWidget {
  final ChatRecordEntity record;
  const _ChatBubbleMe({Key? key, required this.record}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      alignment: Alignment.center,
      margin: EdgeInsets.only(left: R.dimen.dp64, top: R.dimen.dp40),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.end,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Expanded(
            child: Container(
              alignment: Alignment.centerRight,
              child: Stack(
                children: [
                  Container(
                    margin: EdgeInsets.only(right: R.dimen.dp10),
                    padding: EdgeInsets.symmetric(
                        vertical: R.dimen.dp12, horizontal: R.dimen.dp15),
                    decoration: BoxDecoration(
                        color: R.color.ff21b88a,
                        borderRadius: BorderRadius.circular(R.dimen.sp9)),
                    child: Text(
                      record.content,
                      style: TextStyle(
                          fontSize: R.dimen.sp14, color: R.color.ffffffff),
                    ),
                  ),
                  Positioned(
                      top: R.dimen.dp10,
                      right: R.dimen.dp5,
                      child: Image.asset(R.image.chatBubbleMe))
                ],
              ),
            ),
          ),
          Container(
            margin: EdgeInsets.only(right: R.dimen.dp10),
            child: ClipRRect(
              borderRadius: BorderRadius.circular(R.dimen.sp18),
              child: Image.network(
                record.userAvatar,
                width: R.dimen.dp38,
                height: R.dimen.dp38,
                fit: BoxFit.fill,
              ),
            ),
          ),
        ],
      ),
    );
  }
}

class _ChatBubbleOther extends StatelessWidget {
  final ChatRecordEntity record;
  const _ChatBubbleOther({Key? key, required this.record}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      alignment: Alignment.center,
      margin: EdgeInsets.only(right: R.dimen.dp64, top: R.dimen.dp40),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Container(
            margin: EdgeInsets.only(left: R.dimen.dp10),
            child: ClipRRect(
              borderRadius: BorderRadius.circular(R.dimen.sp18),
              child: Image.network(
                record.userAvatar,
                width: R.dimen.dp38,
                height: R.dimen.dp38,
                fit: BoxFit.fill,
              ),
            ),
          ),
          Expanded(
            child: Container(
              alignment: Alignment.centerRight,
              child: Stack(
                children: [
                  Container(
                    margin: EdgeInsets.only(left: R.dimen.dp10),
                    padding: EdgeInsets.symmetric(
                        vertical: R.dimen.dp12, horizontal: R.dimen.dp15),
                    decoration: BoxDecoration(
                        color: R.color.ffffffff,
                        borderRadius: BorderRadius.circular(R.dimen.sp9)),
                    child: Text(
                      record.content,
                      style: TextStyle(
                          fontSize: R.dimen.sp14, color: R.color.ff333333),
                    ),
                  ),
                  Positioned(
                      top: R.dimen.dp10,
                      left: R.dimen.dp5,
                      child: Image.asset(R.image.chatBubbleOther))
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}
